<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends BaseController {

	public function __construct() {
        parent::__construct();
    }

	public function index() {
        $this->display('web'. DIRECTORY_SEPARATOR. "login");
    }

    // 提交方法
    public function login_action(){
        $this->load->model('accountModel');
        // 接收参数
        $username = $this->post("username");
        $password = $this->post("password");
        $captcha = $this->post("captcha");
        // 参数检查
        if($captcha != $this->session->captcha) throw new Exception("请填写正确的验证码");
        if($username != 'admin' || $password != 'aJPHT0ZFetbixuMq') {
            throw new Exception('账号密码错误');
        }
        // 查询数据库
        $userInfo = $this->accountModel->get_admin_user();
        // if(!$userInfo) {
        //     throw new Exception('账号密码错误');
        // }
        // 登录成功，数据写入session
        $this->session->set_userdata("webAdminUser", $userInfo);
        exit(json("登录成功", true));
    }

    // 图像验证码
    public function captcha(){
    	//验证码
		$this->load->library('captcha');
		$code = $this->captcha->getCaptcha();

		// 添加验证码字符串到session
	  	$this->session->set_userdata("captcha", strtolower($code));
	  	$this->captcha->showImg();
    }
}